package games.padinvaders;

import utils.RandomNumber;

import hardware.Display2D;

public class Invader extends SpaceShip{


//public boolean isAlive;


public static final int TYPE_NONE		= 0;
public static final int TYPE_SCOUT		= TYPE_NONE+1;
public static final int TYPE_FIGHTER	= TYPE_SCOUT+1;
public static final int TYPE_BOMBER		= TYPE_FIGHTER+1;
public static final int TYPE_UFO		= TYPE_BOMBER+1;


public static final int LIFEPOINTS_SCOUT		= 5;
public static final int LIFEPOINTS_FIGHTER		= 10;
public static final int LIFEPOINTS_BOMBER		= 20;
public static final int LIFEPOINTS_UFO			= 10;


private int type;


public Invader()
{
	status=STATUS_ALIVE;	
}



public void init(int type)
{
	
	this.type=type;
	switch (type)
	{
	case TYPE_SCOUT:
		lifePoints=5;
	break;
	
	case TYPE_FIGHTER:
		lifePoints=10;
		break;
		
	case TYPE_BOMBER:
		lifePoints=20;
		shieldStrength=20;
		break;
		
	case TYPE_UFO:
		lifePoints=10;
		break;
	
	}
	
	
}




public void draw()
{
	short color=0;
	
	if (!visible)
		return;
	
	
	switch(type)
	{
	case TYPE_SCOUT:
		color=0x0f;
		break;

	case TYPE_FIGHTER:
		color=0x1A;
		break;
	
	case TYPE_BOMBER:	
		if (shieldStrength>0)
			color=(short) 0xf0f;
		else
			color=(short)0x208;
		break;
		
	case TYPE_UFO:
		color=(short)0xff;
		break;
	}
	
	
	switch (status)
		{
		case STATUS_ARIVE:
			arriving();
		break;

		case STATUS_FALLING_A:
		case STATUS_FALLING_B:
			color=0x6;
		
		case STATUS_ALIVE:
			
				Display2D.setPixel((byte)xPos,(byte) yPos, color);
			break;
			
		case STATUS_DESCENDING:
				descending();
			break;	
		
		case STATUS_EXPLODING_A:
				explodeA();
			break;
		
		case STATUS_EXPLODING_B:
				explodeB();
	//			isAlive=false;
			break;	
					

		}
	

}





public void process()
{
	int value;
	
	switch(status)
	{
	case STATUS_ALIVE:
		
		break;
		
		
	case STATUS_EXPLODING_A:
	case STATUS_EXPLODING_B:
		
		break;
	
	case STATUS_EXPLODED:
		if (RandomNumber.getRange(1)>0)
			status=STATUS_FALLING_A;
		else
			status=STATUS_FALLING_B;
		break;

		
		
	case STATUS_FALLING_B:	
	case STATUS_FALLING_A:

		if (yPos<=0)
			{
			status=STATUS_KILLED;
			visible=false;
			}

		if ((animationCounter&0x3)==3)
			{
			yPos--;
			}

			animationCounter++;	
		break;	
	
		
		
	case STATUS_KILLED:
		visible=false;
		break;
		
	}
	
	
}



public void fire(InvaderProjectile invaderProjectile)
{
	int projectileType;
	switch(type)
	{
	case TYPE_SCOUT:
		projectileType=InvaderProjectile.PROJECTILE_TYPE_LASER;	

		
	case TYPE_FIGHTER:
		projectileType=InvaderProjectile.PROJECTILE_TYPE_SHELL;	
		
		break;
	
	case TYPE_BOMBER:	
		projectileType=InvaderProjectile.PROJECTILE_TYPE_BOMB;	

		break;
		
	case TYPE_UFO:
		projectileType=InvaderProjectile.PROJECTILE_TYPE_SUPER_BOMB;	

		break;

		default:
			return;
			
	}

	invaderProjectile.fire(projectileType, xPos, yPos);
}




public void hit(int damage)
{
	if (shieldStrength>0)
		{
		shieldStrength-=damage;

		if (shieldStrength<0)
			lifePoints+=shieldStrength;
		
		}
	else
	{
	lifePoints-=damage;
	}
	
	if (lifePoints<0)
		kill();
	
	
}






public void kill()
{
	int value;
	
	
	value=RandomNumber.get();
	
	value&=0x1;
	switch (value)
	{
	case 0:
		status=STATUS_EXPLODING_A;
		break;
		
	default :
		status=STATUS_EXPLODING_B;
		break;
	}

	animationCounter=0;
}




public void descent()
{
	if (status==STATUS_ALIVE)
		{
		status=STATUS_DESCENDING;
		animationCounter=0;
		}
}





private void descending()
{
	
	animationCounter++;
	
	short value;
	
	
	value=(short)RandomNumber.get();
	
	value&=0x5;
	
	value|=(value<<4);
	value+=0x5;
	
	if (animationCounter<10)
		{
		Display2D.setPixel((byte)xPos,(byte) yPos+1,(short)  value);
		Display2D.setPixel((byte)xPos,(byte) yPos,  color);

		}
	else
	{
		yPos--;

		status=STATUS_ALIVE;
	}
	
	
}








public void arrive()
{
	status=STATUS_ARIVE;
	animationCounter=0;
//	isAlive=true;
	visible=true;
}



private void arriving()
{
	short value;
	
	value =(short) (animationCounter | (animationCounter<<4));
	
	Display2D.setPixel((byte)(xPos),(byte) (yPos), (short) value);

	
	
	animationCounter++;
	if (animationCounter>15)
		{
		status=STATUS_ALIVE;
		}
}



}
